package com.inossem.warehousestorage_inossem.activity.allocation.in_storage;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.utils.log.LogUtils;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.allocation.in_storage.extra.AllocationInStorageExtra;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.OutStorageResultActivity;
import com.inossem.warehousestorage_inossem.adapter.allocation.AllocationInMatsAdapter;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationInMatsBean;
import com.inossem.warehousestorage_inossem.bean.allocation.AllocationInSubmitBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.allocation.AllocationInMatsRequest;
import com.inossem.warehousestorage_inossem.request.allocation.AllocationInSubmitRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.AllocationUtils;
import com.inossem.warehousestorage_inossem.utils.BaseInteface;
import com.inossem.warehousestorage_inossem.utils.OutStorageUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;

import java.util.List;

/**
 * 物料列表
 *
 * @author 詹建宇
 * @time on 2018/4/3 15:41
 * @email jianyu.zhan@inossem.com
 */
public class AllocationInMatsActivity extends BaseActivity implements BaseInteface {

    private TextView title, nextStep;
    private PullableListView listView;

    private String allocate_id;
    private String allocate_code;

    private List<AllocationInMatsBean.MatsBodyBean.ItemListBean> allData;
    private AllocationInMatsAdapter adapter;
    private String stock_input_id;
    private String stock_input_code;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        super.onCreateImpl(savedInstanceState);
        setContentView(R.layout.cy_activity_in_storage_allocation_listview_button);

        initView(this);
        SPUtils.removeAllocationInInformation(this);
    }

    @Override
    protected void onResumeImpl() {
        super.onResumeImpl();
        getExtra(this);
        initData(this);
    }

    private void getExtra(Activity activity) {
        allocate_code = activity.getIntent().getStringExtra(AllocationInStorageExtra.ALLOCATE_CODE);
        allocate_id = activity.getIntent().getStringExtra(AllocationInStorageExtra.ALLOCATE_ID);
    }

    @Override
    public void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        nextStep = (TextView) activity.findViewById(R.id.nextStep);
        listView = (PullableListView) activity.findViewById(R.id.listView);

        listView.setPullUpFlag(false);
        listView.setPullDownFlag(false);
    }

    @Override
    public void initData(Activity activity) {
        title.setText(String.valueOf(allocate_code));
        ActivityUtils.initBackAndFunction(activity, "返回", new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                DialogUtils.getConfirmDialog(activity, getResources().getString(R.string.activity_storage_manage_clear_prompt), new DialogUtils.ConfirmDialogListener() {
                    @Override
                    public void onConfirm(Dialog dialog) {
                        finish();
                    }
                }).show();
            }
        });
        getData(activity);
        nextStep.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                boolean flag = true;
                for (AllocationInMatsBean.MatsBodyBean.ItemListBean itemListBean : allData) {
                    if (!String.valueOf(itemListBean.getIsOk()).equals("1")) {
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    submit(activity);
                } else {
                    Utils.middleToast(activity, "尚有物料没有完成入库");
                }

            }
        });
    }

    private void getData(Activity activity) {
        String json = SPUtils.getAllocationInInformation(activity);
        if (TextUtils.isEmpty(json)) {
            AllocationInMatsRequest.request(activity, allocate_id, new GsonBeanListener<AllocationInMatsBean>() {
                @Override
                public void onSuccess(AllocationInMatsBean bean) {
                    Gson gson = new Gson();
                    String str = gson.toJson(bean, AllocationInMatsBean.class);
                    SPUtils.saveAllocationInInformation(activity, str);
                    allData = bean.getBody().getItem_list();
                    addListData(activity, bean.getBody(), allData, listView, true);
                }
            });
        } else {
            Gson gson = new Gson();
            AllocationInMatsBean bean = gson.fromJson(json, AllocationInMatsBean.class);
            allData = bean.getBody().getItem_list();
            addListData(activity, bean.getBody(), allData, listView, true);
        }
    }

    private void addListData(Activity activity, AllocationInMatsBean.MatsBodyBean bean, List<AllocationInMatsBean.MatsBodyBean.ItemListBean> list, ListView listView, boolean flag) {
        adapter = new AllocationInMatsAdapter(activity, list, bean);
        listView.setAdapter(adapter);
    }

    private void submit(Activity activity) {
        String type = allData.get(0).getMat_store_type();
        switch (type) {
            case OutStorageUtils.TYPE_BATCH:
            case OutStorageUtils.TYPE_NONE:
                //批次 提交
                String jsonBatch = AllocationUtils.submitInStorageBatch(activity, allocate_id, null, null);
                submitBatch(activity, jsonBatch);
                break;
            case OutStorageUtils.TYPE_SN:
                //序列号 提交
                String jsonSN = AllocationUtils.submitInStorageSN(activity, allocate_id, stock_input_id, stock_input_code);
                submitSN(activity, jsonSN);
                break;
        }
    }

    /**
     * 提交 批次
     */
    private void submitBatch(Activity activity, String json) {
        AllocationInSubmitRequest.request(activity, json, new GsonBeanListener<AllocationInSubmitBean>() {
            @Override
            public void onSuccess(AllocationInSubmitBean bean) {
                LogUtils.i("onSuccess:" + bean.getBody());
                SPUtils.removePickingOutStorageInformation(activity);
                if (!TextUtils.isEmpty(bean.getHead().getMsg())) {
                    Utils.middleToast(activity, bean.getHead().getMsg());
                }
                Intent intent = new Intent(activity, OutStorageResultActivity.class);
                startActivity(intent);
            }

            @Override
            public void onFailing(AllocationInSubmitBean bean) {
                super.onFailing(bean);
                stock_input_id = bean.getBody().getStock_input_id();
                stock_input_code = bean.getBody().getStock_input_code();
            }
        });
    }

    /**
     * 提交 序列号
     */
    private void submitSN(Activity activity, String json) {
        AllocationInSubmitRequest.request(activity, json, new GsonBeanListener<AllocationInSubmitBean>() {
            @Override
            public void onSuccess(AllocationInSubmitBean bean) {
                Utils.middleToast(activity, bean.getHead().getMsg());
                finish();
            }

            @Override
            public void onFailing(AllocationInSubmitBean bean) {
                super.onFailing(bean);
                Utils.middleToast(activity, bean.getHead().getMsg());
                stock_input_id = bean.getBody().getStock_input_id();
                stock_input_code = bean.getBody().getStock_input_code();
            }
        });
    }

    @Override
    public void onBackPressed() {
        DialogUtils.getConfirmDialog(this, getResources().getString(R.string.activity_storage_manage_clear_prompt), new DialogUtils.ConfirmDialogListener() {
            @Override
            public void onConfirm(Dialog dialog) {
                finish();
            }
        }).show();
    }
}
